package com.erp.erp_ui.Resource;

import com.erp.erp_entitys.Resource.ResourceEntity;
import com.erp.erp_entitys.Resource.ResourceTreeEntity;
import com.erp.erp_servers.Resource.IResourceService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName ResourceControl
 * @Description 资源控制类
 * @Author songjizhen
 * @Date 2020-06-30 22:54
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "resource")
 public class ResourceControl extends Base {
    @Autowired
    IResourceService resourceService;
    /**
     * 根据主键删除资源
     * @param id
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> delete( Integer id){
        try {
            int num = resourceService.deleteByPrimaryKey(id);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 新增资源
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody ResourceEntity record) {
        try {
            int num = resourceService.insertSelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 查询资源
     * @param
     * @return
     */
    @RequestMapping(value = "selectByPage",method = RequestMethod.POST)
    @ResponseBody

    public R<List<ResourceEntity>> selectByPage(String name,String code,String pid,String pageNum,String pageSize) {
        try {
            ResourceEntity record=new ResourceEntity();
            record.setPid(pid);
            record.setCode(code);
            record.setName(name);
            List<ResourceEntity> resourceEntities = resourceService.selectByExample(record);

            return R.build(resourceEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 查询资源
     * @param
     * @return
     */
    @RequestMapping(value = "selectAll")
    @ResponseBody
    public R<List<ResourceEntity>> selectAll() {
        try {
            ResourceEntity record=new ResourceEntity();
             record.setUrl("all");
            List<ResourceEntity> resourceEntities = resourceService.selectByExample(record);

            return R.build(resourceEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 更新资源
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody ResourceEntity record) {
        try {
            int num = resourceService.updateByPrimaryKeySelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获取资源树
     * @return
     */
    @RequestMapping(value = "getTree",method = RequestMethod.POST)
    @ResponseBody
     public R<List<ResourceTreeEntity>> getTree(){
        try {
            List<ResourceTreeEntity> tree = resourceService.getTree();
            return R.build(tree);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
}
